<?php

  include "DALcustomer.php";

  function insertNewCustomerDetails($title, $surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area1, $ph_no1, $ph_area2, $ph_no2, $card_type1, $card_name1, $card_no1_1, $card_no1_2, $card_no1_3, $card_no1_4, $card_expiry_month1, $card_expiry_year1, $card_type2, $card_name2, $card_no2_1, $card_no2_2, $card_no2_3, $card_no2_4, $card_expiry_month2, $card_expiry_year2)
  {
      switch ($title)
      {
             case "0": $title = "Miss"; break;
             case "1": $title = "Mr"; break;
             case "2": $title = "Mrs"; break;
             case "3": $title = "Ms"; break;
      }
      switch ($card_type1)
      {
             case "Select:" : $card_type1 = NULL; break;
			 case "0": $card_type1 = "Mastercard"; break;
             case "1": $card_type1 = "Visa"; break;
             case "2": $card_type1 = "Amex"; break;
             case "3": $card_type1 = "Diners Club"; break;
      }
      switch ($card_type2)
      {
             case "Select:" : $card_type2 = NULL; break;
             case "0": $card_type2 = "Mastercard"; break;
             case "1": $card_type2 = "Visa"; break;
             case "2": $card_type2 = "Amex"; break;
             case "3": $card_type2 = "Diners Club"; break;
      }
      $dob = $dob_year."-".$dob_month."-".$dob_day;
      $phone1 = $ph_area1.$ph_no1;
      
	  if($ph_area2 == "" || empty($ph_area2) || $ph_no2 == "" || empty($ph_no2))
	  {
        $phone2 = "";
	  }
      else
      {
        $phone2 = $ph_area2.$ph_no2;        
      }

      $card_no1 = $card_no1_1.$card_no1_2.$card_no1_3.$card_no1_4;
      $card_expiry_date1 = $card_expiry_month1.substr($card_expiry_year1, 2);

	  if($card_no2_1 == "" || empty($card_no2_1) || $card_no2_2 == "" || empty($card_no2_2) || $card_no2_3 == "" || empty($card_no2_3) || $card_no2_4 == "" || empty($card_no2_4))
	  {
        $card_no2 = "";
	  }
	  else
	  {
        $card_no2 = $card_no2_1.$card_no2_2.$card_no2_3.$card_no2_4;
	  }
      $card_expiry_date2 = $card_expiry_month2.substr($card_expiry_year2, 2);
      if($card_name2 == "" || empty($card_name2) || $card_no2 == "" || $card_type2 == "")
      {
		$card_expiry_date2 = "";
      }

      return addCustomerDetails($title, $surname, $given_name, $dob, $address, $post_code, $phone1, $phone2, $card_type1, $card_name1, $card_no1, $card_expiry_date1,  $card_type2, $card_name2, $card_no2, $card_expiry_date2);
  }

  function getCustomerDetails($custID, $field)
  {
      return getCustomerDetailsDB($custID, $field);
  }

  function getCustomerDetails2($c_no)
  {
    $customer = getCustomerDetailsDB2($c_no);

    if($customer == "0")
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The customer could not be found.');";
      echo "  window.location.href = 'recordNewCustomerDetails.php';";
      echo "</SCRIPT>";
    }

    $customerInfo[0] = $customer[0];

    $phone1 = $customer[1];

    //phone 1 area no
    for($i=0; $i<2; $i++)
    {
      $customerInfo[1] = $customerInfo[1].$phone1[$i];
    }
    //phone 1 no
    for($i=2; $i<10; $i++)
    {
      $customerInfo[2] = $customerInfo[2].$phone1[$i];
    }

    $phone2 = $customer[2];

    if($phone2 == "")
    {
      $customerInfo[3] = "";
      $customerInfo[4] = "";
    }
	else
    {
      //phone 2 area no
      for($i=0; $i<2; $i++)
      {
        $customerInfo[3] = $customerInfo[3].$phone2[$i];
      }
      for($i=2; $i<10; $i++)
      {
        $customerInfo[4] = $customerInfo[4].$phone2[$i];
      }
    }

    //card type1
    if($customer[3] == "Mast")
      $customerInfo[5] = "Mastercard";
    else if($customer[3] == "Dine")
      $customerInfo[5] = "Diners Club";
    else
      $customerInfo[5] = $customer[3];

    $card_no = $customer[4];
    //card no.1
    for($i=0; $i<4; $i++)
    {
      $customerInfo[6] = $customerInfo[6].$card_no[$i];
    }
    for($i=4; $i<8; $i++)
    {
      $customerInfo[7] = $customerInfo[7].$card_no[$i];
    }
    for($i=8; $i<12; $i++)
    {
      $customerInfo[8] = $customerInfo[8].$card_no[$i];
    }
    for($i=12; $i<16; $i++)
    {
      $customerInfo[9] = $customerInfo[9].$card_no[$i];
    }

    //card name1
    $customerInfo[10] = $customer[5];

    //card expiry date1
    $ex_date1 = $customer[6];

    //expiry date
    for($i=0; $i<2; $i++)
    {
      $customerInfo[11] = $customerInfo[11].$ex_date1[$i];
    }
    for($i=2; $i<4; $i++)
    {
      $customerInfo[12] = $customerInfo[12].$ex_date1[$i];
    }

    if($customer[7] == "")
    {
      $customerInfo[13] = "";
      $customerInfo[14] = "";
      $customerInfo[15] = "";
      $customerInfo[16] = "";
      $customerInfo[17] = "";
      $customerInfo[18] = "";
      $customerInfo[19] = "";
      $customerInfo[20] = "";
    }
    else
    {
      if($customer[7] == "Mast")
        $customerInfo[13] = "Mastercard";
      else if($customer[7] == "Dine")
        $customerInfo[13] = "Diners Club";
      else
        $customerInfo[13] = $customer[7];

      $card_no2 = $customer[8];

      for($i=0; $i<4; $i++)
      {
        $customerInfo[14] = $customerInfo[14].$card_no2[$i];
      }
      for($i=4; $i<8; $i++)
      {
        $customerInfo[15] = $customerInfo[15].$card_no2[$i];
      }
      for($i=8; $i<12; $i++)
      {
        $customerInfo[16] = $customerInfo[16].$card_no2[$i];
      }
      for($i=12; $i<16; $i++)
      {
        $customerInfo[17] = $customerInfo[17].$card_no2[$i];
      }

      $customerInfo[18] = $customer[9];

      $ex_date2 = $customer[10];

      //expiry date
      for($i=0; $i<2; $i++)
      {
        $customerInfo[19] = $customerInfo[19].$ex_date2[$i];
      }
      for($i=2; $i<4; $i++)
      {
        $customerInfo[20] = $customerInfo[20].$ex_date2[$i];
      }
    }

	$customerInfo[21] = $customer[11];

    return $customerInfo;
  }

//this one is for the search part
  function validSearchDetails($surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area, $ph_no, $source)
  {
      if($surname == "" || $given_name == "" || $dob_day == "" || $dob_month == "" || $dob_year == "" || $address == "" || $post_code == "" || $ph_area == "" || $ph_no == ""
        || empty($surname) || empty($given_name) || empty($dob_day) || empty($dob_month) || empty($dob_year) || empty($address) || empty($post_code) || empty($ph_area) || empty($ph_no))
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('You must enter details in all mandatory fields.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($surname) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The surname must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($given_name) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The given name must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($address) > 50)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The address must be less than or equal to 50 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($post_code) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($post_code) != 4)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be 4 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_area) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The area code must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_area) != 2)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The area code must be 2 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_no) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The phone number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_no) != 8)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The phone number must be 8 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else
      {
          $dob = $dob_year."-".$dob_month."-".$dob_day;
          $phone = $ph_area.$ph_no;

          if(checkCustomerDetails($surname, $given_name, $dob, $address, $post_code, $phone) == true)
          {
              if($source == "old")
              {
                  echo "<SCRIPT language='Javascript'>";
                  echo "  alert('The customer is not registered.');";
                  echo "</SCRIPT>";
                  //  put something here to pass cust id, etc to the new customer process
                  //$_SESSION[customerid] =
                  echo "<SCRIPT language='Javascript'>";
                  echo "  window.location.href = 'recordNewCustomerDetails.php';";
                  echo "</SCRIPT>";
              }
              else
              {
                  return true;
              }
              return true;
          }
          else
          {
              if($source == "new")
              {
                  echo "<SCRIPT language='Javascript'>";
                  echo "  alert('The customer is already registered.');";
                  echo "</SCRIPT>";
                  //  put something here to pass cust id, etc to the ordering process
                  //$_SESSION[customerid] =
                  echo "<SCRIPT language='Javascript'>";
                  echo "  window.location.href = 'orderProducts.php';";
                  echo "</SCRIPT>";
              }
              else
              {
                  return true;
              }
              return false;
          }
      }
  }

  function isNumeric($number)
  {
    for($i = 0; $i < strlen($number); $i++)
    {
      $n = $number[$i];
      if($n == '0' || $n == '1' || $n == '2' || $n == '3' || $n == '4' || $n == '5' || $n == '6' || $n == '7' || $n == '8' || $n == '9')
      {
      }
      else
      {
        return false;
      }
    }
    return true;
  }

function validRecordDetails($title, $surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area1, $ph_no1, $ph_area2, $ph_no2, $card_type1, $card_name1, $card_no1_1, $card_no1_2, $card_no1_3, $card_no1_4, $card_expiry_month1, $card_expiry_year1, $card_type2, $card_name2, $card_no2_1, $card_no2_2, $card_no2_3, $card_no2_4, $card_expiry_month2, $card_expiry_year2, $source)
  {
      if($title == "Select:" || $surname == "" || $given_name == "" || $dob_day == "" || $dob_month == "" || $dob_year == "" || $address == "" || $post_code == "" || $ph_area1 == "" || $ph_no1 == "" || $card_type1 == "Select:" || $card_name1 == "" || $card_no1_1 == "" || $card_no1_2 == "" || $card_no1_3 == "" || $card_no1_4 == "" || $card_expiry_month1 == "" || $card_expiry_year1 == ""
        || empty($surname) || empty($given_name) || empty($dob_day) || empty($dob_month) || empty($dob_year) || empty($address) || empty($post_code) || empty($ph_area1) || empty($ph_no1) || empty($card_name1) || empty($card_no1_1) || empty($card_no1_2) || empty($card_no1_3) || empty($card_no1_4) || empty($card_expiry_month1) || empty($card_expiry_year1))
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('You must enter details in all mandatory fields.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($surname) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The surname must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($given_name) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The given name must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($address) > 50)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The address must be less than or equal to 50 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($post_code) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($post_code) != 4)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be 4 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_area1) == false)
      {
        echo $ph_area1;
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first area code must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_area1) != 2 && strlen($ph_area1) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first area code must be 2 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_no1) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first phone number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_no1) != 8 && strlen($ph_no1) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first phone number must be 8 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_area2) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second area code must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_area2) != 2 && strlen($ph_no2) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second area code must be 2 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_no2) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second phone number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_no2) != 8 && strlen($ph_area2) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second phone number must be 8 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($card_name1) > 24)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The preferred credit card name must be less than or equal to 24 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($card_no1_1) == false || isNumeric($card_no1_2) == false || isNumeric($card_no1_3) == false || isNumeric($card_no1_4) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The preferred credit card number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($card_no1_1) > 4 || strlen($card_no1_1) < 4 || strlen($card_no1_2) > 4 || strlen($card_no1_2) < 4 || strlen($card_no1_3) > 4 || strlen($card_no1_3) < 4 || strlen($card_no1_4) > 4 || strlen($card_no1_4) < 4)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The preferred credit card number sections must be 4 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($card_name2) > 24)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The alternate credit card name must be less than or equal to 24 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($card_no2_1) == false || isNumeric($card_no2_2) == false || isNumeric($card_no2_3) == false || isNumeric($card_no2_4) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The alternate credit card number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($card_no2_1) > 4 || strlen($card_no2_1) < 4 || strlen($card_no2_2) > 4 || strlen($card_no2_2) < 4 || strlen($card_no2_3) > 4 || strlen($card_no2_3) < 4 || strlen($card_no2_4) > 4 || strlen($card_no2_4) < 4)
      {
           if($card_no2_1 != "" || $card_no2_2 != "" || $card_no2_3 != "" || $card_no2_4 != "")
           {
               echo "<SCRIPT language='Javascript'>";
               echo "  alert('The alternate credit card number sections must be 4 digits long.');";
               echo "</SCRIPT>";
               return false;
           }
           else
           {
               return true;
           }
      }
      return true;
  }

  function validChangeDetails($title, $surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area1, $ph_no1, $ph_area2, $ph_no2, $card_type1, $card_name1, $card_no1_1, $card_no1_2, $card_no1_3, $card_no1_4, $card_expiry_month1, $card_expiry_year1, $card_type2, $card_name2, $card_no2_1, $card_no2_2, $card_no2_3, $card_no2_4, $card_expiry_month2, $card_expiry_year2)
  {
      if(strlen($surname) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The surname must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($given_name) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The given name must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($address) > 50)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The address must be less than or equal to 50 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($post_code) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
           echo "here";
      if(strlen($post_code) != 4 && strlen($post_code) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be 4 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($ph_area1) == false)
      {
           echo $ph_area1;
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first area code must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($ph_area1) != 2 && strlen($ph_area1) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first area code must be 2 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($ph_no1) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first phone number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($ph_no1) != 8 && strlen($ph_no1) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The first phone number must be 8 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($ph_area2) == false)
      {
           echo $ph_area1;
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second area code must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($ph_area2) != 2 && strlen($ph_area2) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second area code must be 2 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($ph_no2) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second phone number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($ph_no2) != 8 && strlen($ph_no2) != 0)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The second phone number must be 8 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($card_name1) > 24)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The preferred credit card name must be less than or equal to 24 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($card_no1_1) == false || isNumeric($card_no1_2) == false || isNumeric($card_no1_3) == false || isNumeric($card_no1_4) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The preferred credit card number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($card_no1_1) > 4 || strlen($card_no1_1) < 4 || strlen($card_no1_2) > 4 || strlen($card_no1_2) < 4 || strlen($card_no1_3) > 4 || strlen($card_no1_3) < 4 || strlen($card_no1_4) > 4 || strlen($card_no1_4) < 4)
      {
           if($card_no1_1 != "" || $card_no1_2 != "" || $card_no1_3 != "" || $card_no1_4 != "")
           {
               echo "<SCRIPT language='Javascript'>";
               echo "  alert('The preferred credit card number sections must be 4 digits long.');";
               echo "</SCRIPT>";
               return false;
           }
      }
      if(strlen($card_name2) > 24)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The alternate credit card name must be less than or equal to 24 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      if(isNumeric($card_no2_1) == false || isNumeric($card_no2_2) == false || isNumeric($card_no2_3) == false || isNumeric($card_no2_4) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The alternate credit card number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      if(strlen($card_no2_1) > 4 || strlen($card_no2_1) < 4 || strlen($card_no2_2) > 4 || strlen($card_no2_2) < 4 || strlen($card_no2_3) > 4 || strlen($card_no2_3) < 4 || strlen($card_no2_4) > 4 || strlen($card_no2_4) < 4)
      {
           if($card_no2_1 != "" || $card_no2_2 != "" || $card_no2_3 != "" || $card_no2_4 != "")
           {
               echo "<SCRIPT language='Javascript'>";
               echo "  alert('The alternate credit card number sections must be 4 digits long.');";
               echo "</SCRIPT>";
               return false;
           }
      }
      return true;
  }

  function updateCustomerDetails($title, $surname, $given_name, $dob, $address, $post_code, $phone1, $phone2, $card_type1, $card_name1, $card_no1, $card_expiry_date1, $card_type2, $card_name2, $card_no2, $card_expiry_date2)
  {
    if(lockCustomerTable())
    {
      $old_dob = $_SESSION["old_dob_year"]."-".$_SESSION["old_dob_month"]."-".$_SESSION["old_dob_day"];
      $old_phone1 = $_SESSION["old_ph_area1"].$_SESSION["old_ph_no1"];
      $old_phone2 = $_SESSION["old_ph_area2"].$_SESSION["old_ph_no2"];
      $old_card_no1 = $_SESSION["old_card_no1_1"].$_SESSION["old_card_no1_2"].$_SESSION["old_card_no1_3"].$_SESSION["old_card_no1_4"];
      $old_card_no2 = $_SESSION["old_card_no2_1"].$_SESSION["old_card_no2_2"].$_SESSION["old_card_no2_3"].$_SESSION["old_card_no2_4"];
      $old_card_expiry_date1 = $_SESSION["old_card_expiry_month1"].$_SESSION["old_card_expiry_year1"];
      $old_card_expiry_date2 = $_SESSION["old_card_expiry_month2"].$_SESSION["old_card_expiry_year2"];

      if($_SESSION["old_title"] == getCustomerDetailsDB($_SESSION["customerID"], "title")
        && $_SESSION["old_surname"] == getCustomerDetailsDB($_SESSION["customerID"], "surname")
        && $_SESSION["old_given_name"] == getCustomerDetailsDB($_SESSION["customerID"], "given_name")
        && $old_dob == getCustomerDetailsDB($_SESSION["customerID"], "date_of_birth")
        && $_SESSION["old_address"] == getCustomerDetailsDB($_SESSION["customerID"], "address")
        && $_SESSION["old_post_code"] == getCustomerDetailsDB($_SESSION["customerID"], "pcode")
        && $old_phone1 == getCustomerDetailsDB($_SESSION["customerID"], "phone1")
        && $old_phone2 == getCustomerDetailsDB($_SESSION["customerID"], "phone2")
        && $_SESSION["old_card_type1"] == getCustomerDetailsDB($_SESSION["customerID"], "card_type1")
        && $old_card_no1 == getCustomerDetailsDB($_SESSION["customerID"], "card_no1")
        && $_SESSION["old_card_name1"] == getCustomerDetailsDB($_SESSION["customerID"], "card_name1")
        && $old_card_expiry_date1 == getCustomerDetailsDB($_SESSION["customerID"], "card_expiry_date1")
        && $_SESSION["old_card_type2"] == getCustomerDetailsDB($_SESSION["customerID"], "card_type2")
        && $old_card_no2 == getCustomerDetailsDB($_SESSION["customerID"], "card_no2")
        && $_SESSION["old_card_name2"] == getCustomerDetailsDB($_SESSION["customerID"], "card_name2")
        && $old_card_expiry_date2 == getCustomerDetailsDB($_SESSION["customerID"], "card_expiry_date2"))
      {
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "title", $title);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "surname", $surname);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "given_name", $given_name);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "date_of_birth", $dob);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "address", $address);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "pcode", $post_code);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "phone1", $phone1);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "phone2", $phone2);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_type1", $card_type1);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_no1", $card_no1);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_name1", $card_name1);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_expiry_date1", $card_expiry_date1);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_type2", $card_type2);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_no2", $card_no2);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_name2", $card_name2);
        $success = updateCustomerDetailsDB($_SESSION["customerID"], "card_expiry_date2", $card_expiry_date2);
      }
      else
      {
        $_SESSION["concurrency_error"] = true;
        echo "<SCRIPT language='Javascript'>";
        echo "  alert('Original customer details in the database have been modified during the updating process. Please restart process.');";
        echo "  window.location.href = 'changeCustomerDetails.php';";
        echo "</SCRIPT>";
      }
    }
    else
    {
      $success = false;
    }
    unlockCustomerTable();
    return $success;
  }

  function searchCustomer($surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area, $ph_no)
  {
      if($surname == "" || $given_name == "" || $dob_day == "" || $dob_month == "" || $dob_year == "" || $address == "" || $post_code == "" || $ph_area == "" || $ph_no == ""
        || empty($surname) || empty($given_name) || empty($dob_day) || empty($dob_month) || empty($dob_year) || empty($address) || empty($post_code) || empty($ph_area) || empty($ph_no))
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('You must enter details in all mandatory fields.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($surname) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The surname must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($given_name) > 30)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The given name must be less than or equal to 30 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($address) > 50)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The address must be less than or equal to 50 characters long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($post_code) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($post_code) != 4)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The postcode must be 4 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_area) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The area code must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_area) != 2)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The area code must be 2 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(isNumeric($ph_no) == false)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The phone number must be numeric.');";
           echo "</SCRIPT>";
           return false;
      }
      else if(strlen($ph_no) != 8)
      {
           echo "<SCRIPT language='Javascript'>";
           echo "  alert('The phone number must be 8 digits long.');";
           echo "</SCRIPT>";
           return false;
      }
      else
      {
         return true;
      }
  }

  function checkCustomerDuplicates($surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area, $ph_no)
  {
    $dob = $dob_year."-".$dob_month."-".$dob_day;
    $phone = $ph_area.$ph_no;

    if(checkCustomerDetails($surname, $given_name, $dob, $address, $post_code, $phone) == false)
    {
      return true;
    }
    else
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('The customer has not been registered.');";
      echo "</SCRIPT>";
      return false;
    }
  }
  
  function getCustomerNo($surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $post_code, $ph_area, $ph_no)
  {
    $dob = $dob_year."-".$dob_month."-".$dob_day;
    $phone = $ph_area.$ph_no;

    return searchCustomerNo($surname, $given_name, $dob, $address, $post_code, $phone);
  }

  function getPreferredCredit($c_no)
  {
    $card = findPreferredDetails($c_no);

    //card type
    if($card[0] == "Mast")
      $card_details[0] = "Mastercard";
    else if($card[0] == "Dine")
      $card_details[0] = "Diners Club";
    else
      $card_details[0] = $card[0];

    $card_details[1] = $card[1];

    $card_no = $card[2];
    
    for($i=0; $i<4; $i++)
    {
      $card_details[2] = $card_details[2].$card_no[$i];
    }
    for($i=4; $i<8; $i++)
    {
      $card_details[3] = $card_details[3].$card_no[$i];
    }
    for($i=8; $i<12; $i++)
    {
      $card_details[4] = $card_details[4].$card_no[$i];
    }
    for($i=12; $i<16; $i++)
    {
      $card_details[5] = $card_details[5].$card_no[$i];
    }

    //card expiry date
    $ex_date = $card[3];

    //expiry date
    for($i=0; $i<2; $i++)
    {
      $card_details[6] = $card_details[6].$ex_date[$i];
    }
    for($i=2; $i<4; $i++)
    {
      $card_details[7] = $card_details[7].$ex_date[$i];
    }
    
    return $card_details;
  }
 
  function getAlternateCredit($c_no)
  {
    $card = findAlternateDetails($c_no);
 
     //card type
    if($card[0] == "Mast")
      $card_details[0] = "Mastercard";
    else if($card[0] == "Dine")
      $card_details[0] = "Diners Club";
    else
      $card_details[0] = $card[0];

    $card_details[1] = $card[1];

    $card_no = $card[2];
    
    for($i=0; $i<4; $i++)
    {
      $card_details[2] = $card_details[2].$card_no[$i];
    }
    for($i=4; $i<8; $i++)
    {
      $card_details[3] = $card_details[3].$card_no[$i];
    }
    for($i=8; $i<12; $i++)
    {
      $card_details[4] = $card_details[4].$card_no[$i];
    }
    for($i=12; $i<16; $i++)
    {
      $card_details[5] = $card_details[5].$card_no[$i];
    }

    //card expiry date
    $ex_date = $card[3];

    //expiry date
    for($i=0; $i<2; $i++)
    {
      $card_details[6] = $card_details[6].$ex_date[$i];
    }
    for($i=2; $i<4; $i++)
    {
      $card_details[7] = $card_details[7].$ex_date[$i];
    }
    
    return $card_details;
  }

?>